package jfox.platform.aaa.action;

import java.util.List;
import javax.ejb.EJB;

import jfox.platform.aaa.bo.RoleBO;
import jfox.platform.aaa.entity.Role;
import jfox.platform.common.Utils;
import jfox.platform.function.bo.NodeBO;
import jfox.platform.function.entity.Node;
import jfox.platform.infrastructure.SuperAction;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;
import org.jfox.mvc.validate.StringValidation;

/**
 * @author <a href="mailto:thaiguo@gmail.com">Taiguo Zhang</a>
 */
@Service(id = "role")
public class RoleAction extends SuperAction {

    @EJB
    RoleBO roleBO;

    @EJB
    NodeBO nodeBO;

    @ActionMethod(name = "mgmt", successView = "platform/aaa/role_management.vhtml")
    public void listRole(ActionContext actionContext) throws Exception {
        List<Role> roles = roleBO.getAllRoles();
//        Collections.sort(roles);
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("roles", roles);
    }

    @ActionMethod(name = "newview", successView = "platform/aaa/role_new.vhtml")
    public void newRoleView(ActionContext actionContext) throws Exception {
        List<Node> nodes = nodeBO.getAllNodes();
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("nodes", nodes);
    }

    @ActionMethod(name = "editview", successView = "platform/aaa/role_edit.vhtml", invocationClass = GetRoleInvocation.class)
    public void editRoleView(ActionContext actionContext) throws Exception {
        GetRoleInvocation invocation = (GetRoleInvocation)actionContext.getInvocation();
        Role role = roleBO.getRoleById(invocation.getId());
        List<Node> nodes = nodeBO.getAllNodes();
        PageContext pageContext = actionContext.getPageContext();
        pageContext.setAttribute("nodes", nodes);
        pageContext.setAttribute("role", role);
    }

    @ActionMethod(name = "new", successView = "include/json_response.vhtml", errorView = "include/json_response.vhtml", invocationClass = NewRoleInvocation.class)
    public synchronized void newRole(ActionContext actionContext) throws Exception {
        NewRoleInvocation newRoleInvocation = (NewRoleInvocation)actionContext.getInvocation();
        Role role = new Role();
        role.setName(newRoleInvocation.getName());
        role.setVstatus(newRoleInvocation.getVstatus());
        role.setVersion(0);
        role.setPriority(newRoleInvocation.getPriority());
        role.setLocalName(newRoleInvocation.getLocalName());
        role.setRefs(newRoleInvocation.getRefs());
        role.setDescription(newRoleInvocation.getDescription());
        role.setLastModifier(getUserContext().getUsername());
        role.setNodes(newRoleInvocation.getNodesString());

        role.setCreator(getUserContext().getUsername());
		roleBO.newRole(role);

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, role);

    }

    @ActionMethod(name = "edit", successView = "include/json_response.vhtml", errorView = "include/json_response.vhtml", invocationClass = EditRoleInvocation.class)
    public synchronized void editRole(ActionContext actionContext) throws Exception {
        EditRoleInvocation editRoleInvocation = (EditRoleInvocation)actionContext.getInvocation();

        Role role = roleBO.getRoleById(editRoleInvocation.getId());
        role.setName(editRoleInvocation.getName());
        role.setVstatus(editRoleInvocation.getVstatus());
        role.setVersion(role.getVersion()+1);
        role.setPriority(editRoleInvocation.getPriority());
        role.setLocalName(editRoleInvocation.getLocalName());
        role.setRefs(editRoleInvocation.getRefs());
        role.setDescription(editRoleInvocation.getDescription());
        role.setLastModifier(getUserContext().getUsername());
        role.setNodes(editRoleInvocation.getNodesString());
        roleBO.updateRole(role);

        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, role);
    }

    @ActionMethod(name = "delete", successView = "include/json_response.vhtml", errorView = "include/json_response.vhtml", invocationClass = GetRoleInvocation.class)
    public synchronized void deleteRole(ActionContext actionContext) throws Exception {
        GetRoleInvocation getRoleInvocation = (GetRoleInvocation)actionContext.getInvocation();
        roleBO.deleteRole(getRoleInvocation.getId());
        actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, getRoleInvocation.getId());
    }

    public static class NewRoleInvocation extends Invocation {

        @StringValidation(nullable = false,minLength = 2)
        private String name;

        private String creator;

        private long createTime = 0;

        private int priority = 0;

        private int vstatus = 0;

        private String refs = "";

        private String localName = "";

        private String description = "";

        private long[] nodes;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCreator() {
            return creator;
        }

        public void setCreator(String creator) {
            this.creator = creator;
        }

        public long getCreateTime() {
            return createTime;
        }

        public void setCreateTime(long createTime) {
            this.createTime = createTime;
        }

        public int getPriority() {
            return priority;
        }

        public void setPriority(int priority) {
            this.priority = priority;
        }

        public int getVstatus() {
            return vstatus;
        }

        public void setVstatus(int vstatus) {
            this.vstatus = vstatus;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public void setNodes(long[] nodes) {
            this.nodes = nodes;
        }

        public long[] getNodes() {
            return nodes;
        }

        public String getNodesString(){
            return Utils.array2String(getNodes());
        }

        public String getRefs() {
            return refs;
        }

        public void setRefs(String refs) {
            this.refs = refs;
        }

        public String getLocalName() {
            return localName;
        }

        public void setLocalName(String localName) {
            this.localName = localName;
        }
    }

    public static class GetRoleInvocation extends Invocation {

        private long id;

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }
    }

    public static class EditRoleInvocation extends NewRoleInvocation {

        private long id;

        public long getId() {
            return id;
        }

        public void setId(long id) {
            this.id = id;
        }

    }
}
